﻿using System;
using System.Collections.Generic;
using System.Linq;
using DynamicLinqExtensions;
using DynamicLinqExtensions.Exceptions;
using DynamicLinqExtensionsTest.ModelTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DynamicLinqExtensionsTest
{
    [TestClass]
    public class GeneralExtensionsTest
    {
        [TestMethod]
        public void GetTypeFromIQuerableTest_IQueryableOfPerson_ReturnsPersonType()
        {
            var testClasses = new List<Person>().AsQueryable();
            Assert.IsTrue(testClasses.GetTypeFromIQueryable() == typeof (Person));
        }

        [TestMethod]
        public void GetPropertyInfo_ModelWithProperty_ReturnsValidPropertyInfo()
        {
            var testClasses = new List<Person>().AsQueryable();
            var propertyInfo = testClasses.GetPropertyInfo("FirstName");
            Assert.IsTrue(propertyInfo.Name == "FirstName");
        }

        [TestMethod]
        [ExpectedException(typeof(DynamicLinqExtensionsException))]
        public void GetPropertyInfo_ModelWithoutProperty_ThrowsException()
        {
            var testClasses = new List<Person>().AsQueryable();
            var propertyInfo = testClasses.GetPropertyInfo("InvalidPropertyName");
        }

        [TestMethod]
        public void Single_IEnumerableWithOneItem_ReturnsItem()
        {
            var people = new List<Person>();
            people.Add(new Person());
            var result = ((IQueryable) people.AsQueryable()).Single();
            Assert.IsTrue(result is Person);
        }

        [TestMethod]
        [ExpectedException(typeof(DynamicLinqExtensionsException))]
        public void Single_IEnumerableWithTwoItems_ThrowsException()
        {
            var people = new List<Person>();
            people.Add(new Person());
            people.Add(new Person());
            var result = ((IQueryable)people.AsQueryable()).Single();
            Assert.IsTrue(result is Person);
        }

        [TestMethod]
        [ExpectedException(typeof(DynamicLinqExtensionsException))]
        public void Single_IEnumerableWithNoItems_ThrowsException()
        {
            var people = new List<Person>();
            var result = ((IQueryable)people.AsQueryable()).Single();
            Assert.IsTrue(result is Person);
        }
    }

    
}
